package com.source.hotelbath.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.WindowManager;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 手机辅助类
 * <p/>
 * 创建人:
 * 版本号:2.8.0
 * 修改人:
 * 修改时间:
 * 修改内容:
 */
public class PhoneUtils {

    /**
     * 获取当前年份
     */
    public static String getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String date = sdf.format(new java.util.Date());
        return date;
    }

    /**
     * 获取当前年月日
     */
    public static String getCurrentYearDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date = sdf.format(new java.util.Date());
        return date;
    }

    /**
     * 判断当前是否有网络
     *
     * @param context
     * @return true 有网
     */
    public static boolean getNetInfo(Context context) {
        // 获取系统服务中的ConnectivityManager对象
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取网络连接对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo == null) {
            return false;
        }
        return true;
    }

    public static int getWindowWidth(Context context) {
        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
        @SuppressWarnings("deprecation")
        int width = display.getWidth();
        return width;
    }

    public static int getWindowHeight(Context context) {
        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
        @SuppressWarnings("deprecation")
        int height = display.getHeight();
        return height;
    }

    //获取屏幕的宽度 
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

    // 获取屏幕的高度
    public static int getScreenHeight(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getHeight();
    }

    /**
     * 根据手机的分辨率 从dp 转成px
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(float dpValue, Context context) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从px 转 dp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dip(float pxValue, Context context) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 获取设备信息
     *
     * @param context 上下文对象
     * @return
     */
    public static String getDevice(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();//唯一设备的Id
        String appName = getAppName(context);
        String appVersion = getVersionName(context);
        String appScreen = getWindowWidth(context) + "x" + getWindowHeight(context);
        String systemName = android.os.Build.VERSION.INCREMENTAL;
        String deviceName = android.os.Build.MODEL;
        String systemVersion = android.os.Build.VERSION.RELEASE;
        String message = "systemName:" + systemName + ";" + "systemVersion:" + systemVersion + ";" + "deviceName:" + deviceName + ";"
                + "DeviceId:" + deviceId + ";" + "appName:" + appName + ";" + "appVersion:" + appVersion + ";" + "appScreen:" + appScreen;
        return message;
    }

    public static String getMD5(String info) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(info.getBytes("UTF-8"));
            byte[] encryption = md5.digest();

            StringBuffer strBuf = new StringBuffer();
            for (int i = 0; i < encryption.length; i++) {
                if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
                    strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
                } else {
                    strBuf.append(Integer.toHexString(0xff & encryption[i]));
                }
            }

            return strBuf.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * 获取手机设备id,在请求接口问答、软件更新、登录时用到
     *
     * @param context
     * @return
     */
    public static String getDeviceDDID(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();
        String appScreen = getWindowWidth(context) + "-" + getWindowHeight(context);
        String deviceName = android.os.Build.MODEL;
        String message = getMD5(deviceName + deviceId + appScreen);
        return message.toUpperCase();
    }

    /**
     * 获取当前的版本号
     *
     * @param context
     * @return
     */
    public static String getVersionName(Context context) {

        try {
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取当前的版本号
     *
     * @param context
     * @return
     */
    public static String getAppName(Context context) {

        try {
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
            return (String) packageManager.getApplicationLabel(applicationInfo);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return "0";
        }
    }
}
